﻿using BasePlatformCommon;
using BasePlatformNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace L9SubSet
{
    public partial class Form_AmpControl : Form
    {
        private Form_MainSet parent = null;
        private BaseCoreNet baseCoreNet = null;

        private int devIndex = -1;
        private int subIndex = 0;
        private int portIndex = 0;
        private int miniIndex = -1;

        private bool forceUpadteEnable = false;
        private int forceInitSendNum = 0;

        private string updateSrcFilePath = "";			// 升级源文件

        private bool updateMcuProcessing = false;		// 解码器升级进行中
        private bool forceStopUpdateMcu = false;		// 强制停止升级

        // 委托定义与声明
        private delegate void PrintLogDelegate(object o);
        private PrintLogDelegate printLogDelegate;
        private delegate void StartUpdateMcuDelegate();
        private StartUpdateMcuDelegate startUpdateMcuDelegate;
        private delegate void UpdateMcuUpdateProcessDelegate(int value);
        private UpdateMcuUpdateProcessDelegate updateMcuUpdateProcessDelegate;
        private delegate void AlertUpdateDoneMessageDelegate();
        private AlertUpdateDoneMessageDelegate alertUpdateDoneMessageDelegate;

        private System.Timers.Timer forceInitTimer = null;

        private int mcuMode = 1;

        private string section = "AmpControl";			// 多语言文件配置节名称

        private string tip = "提示";						// MessageBox 题头

        private bool TestFlag = false;
        /*****************************************************************************************/

		// 构造函数
        public Form_AmpControl(Form_MainSet parent, BaseCoreNet baseCoreNet)
        {
            InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.parent = parent;
			this.baseCoreNet = baseCoreNet;

			initEnvironment();

            this.receiverControlCheckBox.Checked = true;
        }

        // 初始化运行环境
        private void initEnvironment()
        {
            printLogDelegate = new PrintLogDelegate(printLog);
            startUpdateMcuDelegate = new StartUpdateMcuDelegate(startUpdateMcu);
            updateMcuUpdateProcessDelegate = new UpdateMcuUpdateProcessDelegate(updateMcuUpdateProcess);
            alertUpdateDoneMessageDelegate = new AlertUpdateDoneMessageDelegate(alertUpdateDoneMessage);

            subIndexTextBox.Text = "1";
            portIndexTextBox.Text = "1";
            miniIndexTextBox.Text = "1";

            forceInitTimer = new System.Timers.Timer();
            forceInitTimer.Interval = 25;
            forceInitTimer.Elapsed += new System.Timers.ElapsedEventHandler(forceInitTimer_Elapsed);

            // 设置本地化语言
            if (LanguageOper.available == true)
            {
                setLanguageText();
            }

            baseCoreNet.setLampCheckProcessing(true);
        }

        // 设置本地化语言
        public void setLanguageText()
        {
            if (LanguageOper.available == false)
            {
                return;
            }

            LanguageOper.SetCurrentCulture();

            tip = LanguageOper.ReadValue("Common", "tipStr");

            // 界面控件文字
            this.Text = LanguageOper.ReadValue(section, "recControl.Text");
            subGroupBox.Text = LanguageOper.ReadValue(section, "subGroupBox.Text");
            portGroupBox.Text = LanguageOper.ReadValue(section, "portGroupBox.Text");
            recGroupBox.Text = LanguageOper.ReadValue(section, "recGroupBox.Text");
            controlGroupBox.Text = LanguageOper.ReadValue(section, "controlGroupBox.Text");
            receiverControlCheckBox.Text = LanguageOper.ReadValue(section, "receiverControlCheckBox.Text");
            getInfoGroupBox.Text = LanguageOper.ReadValue(section, "getInfoGroupBox.Text");
            getReceiverInfoButton.Text = LanguageOper.ReadValue(section, "getReceiverInfoButton.Text");
            modeGroupBox.Text = LanguageOper.ReadValue(section, "modeGroupBox.Text");
            setUpdateModeButton.Text = LanguageOper.ReadValue(section, "setUpdateModeButton.Text");
            sendForceUpadteCmdButton.Text = LanguageOper.ReadValue(section, "sendForceUpadteCmdButton.Text");
            updateRecGroupBox.Text = LanguageOper.ReadValue(section, "updateRecGroupBox.Text");
            fileLabel.Text = LanguageOper.ReadValue(section, "fileLabel.Text");
            //updateModeLabel.Text = LanguageOper.ReadValue(section, "updateModeLabel.Text");
            processLabel.Text = LanguageOper.ReadValue(section, "processLabel.Text");
            openUpdateSrcFileButton.Text = LanguageOper.ReadValue(section, "openUpdateSrcFileButton.Text");
            updateReceiverButton.Text = LanguageOper.ReadValue(section, "updateReceiverButton.Text");
            logGroupBox.Text = LanguageOper.ReadValue(section, "logGroupBox.Text");
            clearLogButton.Text = LanguageOper.ReadValue(section, "clearLogButton.Text");
            closeButton.Text = LanguageOper.ReadValue(section, "closeButton.Text");
        }

        // 获取通用参数：活跃设备、分控、端口、放大器
        private bool getCommonParameter()
        {
            // 活跃设备
            devIndex = baseCoreNet.getActiveDevIndex();
            if (devIndex == -1)
            {
                string msg = "当前未检测到分控";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.0");
                }

                MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

                return false;
            }

            // 分控
            if (allSubCheckBox.Checked)
            {
                subIndex = 0;
            }
            else
            {
                // 检查分控序号
                int tmpSubIndex = -1;
                bool parseRes = int.TryParse(subIndexTextBox.Text.Trim(), out tmpSubIndex);
                if (parseRes == false || tmpSubIndex < 1 || tmpSubIndex > 255)
                {
                    string msg = "分控序号解析错误, 应为 [1,255] 之间的整数";
                    if (LanguageOper.available == true)
                    {
                        msg = LanguageOper.ReadValue(section, "msgStr.1");
                    }

                    MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return false;
                }
                else
                {
                    subIndex = tmpSubIndex;
                }
            }

            // 端口
            if (allPortCheckBox.Checked)
            {
                portIndex = 0;
            }
            else
            {
                // 检查端口序号
                int tmpPortIndex = -1;
                bool parseRes = int.TryParse(portIndexTextBox.Text.Trim(), out tmpPortIndex);
                if (parseRes == false || tmpPortIndex < 1 || tmpPortIndex > 8)
                {
                    string msg = "端口序号解析错误, 应为 [1,8] 之间的整数";
                    if (LanguageOper.available == true)
                    {
                        msg = LanguageOper.ReadValue(section, "msgStr.2");
                    }

                    MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return false;
                }
                else
                {
                    portIndex = tmpPortIndex;
                }
            }

            // 放大器
            if (allMiniCheckBox.Checked)
            {
                miniIndex = -1;
            }
            else
            {
                // 检查放大器序号
                int tmpMiniIndex = -1;
                bool parseRes = int.TryParse(miniIndexTextBox.Text.Trim(), out tmpMiniIndex);
                if (parseRes == false || tmpMiniIndex < 1 || tmpMiniIndex > 65535)
                {
                    string msg = "放大器序号解析错误, 应为 [1,65535] 之间的整数";
                    if (LanguageOper.available == true)
                    {
                        msg = LanguageOper.ReadValue(section, "msgStr.3");
                    }

                    MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return false;
                }
                else
                {
                    miniIndex = tmpMiniIndex;
                }
            }

            return true;
        }

        // 获取/解除放大器控制权
        private void receiverControlCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            baseCoreNet.setControlState(subIndex, receiverControlCheckBox.Checked, pixelDetectCheckBox.Checked);
            //TOTEST

            getReceiverInfoButton.Enabled = receiverControlCheckBox.Checked;
            openUpdateSrcFileButton.Enabled = receiverControlCheckBox.Checked;
            updateReceiverButton.Enabled = receiverControlCheckBox.Checked;
            setUpdateModeButton.Enabled = receiverControlCheckBox.Checked;
            sendForceUpadteCmdButton.Enabled = receiverControlCheckBox.Checked;

            if (receiverControlCheckBox.Checked)
            {
                string msg = "已获取放大器控制权";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.4");
                }
                printLog(msg);
            }
            else
            {
                string msg = "已解除放大器控制权";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.5");
                }
                printLog(msg);
            }
        }

        // 获取/解除解码器控制权 - pixel
        private void pixelDetectCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            baseCoreNet.setControlState(subIndex, receiverControlCheckBox.Checked, pixelDetectCheckBox.Checked);
            //TOTEST
        }

        // 设置升级模式
        private void setUpdateModeButton_Click(object sender, EventArgs e)
        {
            if (getCommonParameter() == false)
            {
                return;
            }

            // 升级模式 - Reset
            if (resetModeRadioButton.Checked)
            {
                //baseCoreNet.setMcuReset(subIndex, portIndex, miniIndex);
                //TOTEST
                baseCoreNet.setAmpReset(subIndex, portIndex);
                string msg = "设置放大器模式：Reset";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.6");
                }
                this.BeginInvoke(printLogDelegate, msg);
            }
            // 升级模式 - Init
            else if (initModeRadioButton.Checked)
            {
                //baseCoreNet.setMcuInit(subIndex, portIndex, miniIndex);
                //TOTEST
                baseCoreNet.setAmpInit(subIndex, portIndex);
                string msg = "设置放大器模式：Init";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.7");
                }
                this.BeginInvoke(printLogDelegate, msg);
            }
            // 升级模式 - Active
            else if (activeModeRadioButton.Checked)
            {
                //baseCoreNet.setMcuActive(subIndex, portIndex, miniIndex);
                //TOTEST
                baseCoreNet.setAmpActive(subIndex, portIndex);
                string msg = "设置放大器模式：Active";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.8");
                }
                this.BeginInvoke(printLogDelegate, msg);
            }
        }
        // 强制升级
        private void sendForceUpadteCmdButton_Click(object sender, EventArgs e)
        {
            if (getCommonParameter() == false)
            {
                return;
            }

            string text1 = "停止强制升级";
            if (LanguageOper.available == true)
            {
                text1 = LanguageOper.ReadValue(section, "sendForceUpadteCmdButton.Text.0");
            }
            string text2 = "强制升级";
            if (LanguageOper.available == true)
            {
                text2 = LanguageOper.ReadValue(section, "sendForceUpadteCmdButton.Text.1");
            }

            if (forceUpadteEnable == false)
            {
                forceInitSendNum = 0;
                forceInitTimer.Start();
                forceUpadteEnable = true;

                subGroupBox.Enabled = false;
                portGroupBox.Enabled = false;
                recGroupBox.Enabled = false;
                controlGroupBox.Enabled = false;
                getInfoGroupBox.Enabled = false;

                getReceiverInfoButton.Enabled = false;
                openUpdateSrcFileButton.Enabled = false;
                updateReceiverButton.Enabled = false;
                setUpdateModeButton.Enabled = false;

                sendForceUpadteCmdButton.Text = text1;
            }
            else
            {
                forceInitTimer.Stop();
                forceUpadteEnable = false;

                subGroupBox.Enabled = true;
                portGroupBox.Enabled = true;
                recGroupBox.Enabled = true;
                controlGroupBox.Enabled = true;
                getInfoGroupBox.Enabled = true;

                getReceiverInfoButton.Enabled = true;
                openUpdateSrcFileButton.Enabled = true;
                updateReceiverButton.Enabled = true;
                setUpdateModeButton.Enabled = true;

                sendForceUpadteCmdButton.Text = text2;
            }
        }

        // 周期发送强制升级命令
        private void forceInitTimer_Elapsed(object sender, EventArgs e)
        {
            //baseCoreNet.setMcuInit(subIndex, portIndex, miniIndex);
            //TOTEST
            baseCoreNet.setAmpInit(subIndex, portIndex);

            forceInitSendNum++;
            if (forceInitSendNum > 1000000)
            {
                forceInitSendNum = 0;
            }

            string msg = string.Format("强制升级：发送 Init 命令, LoopNum = {0}", forceInitSendNum);
            if (LanguageOper.available == true)
            {
                msg = string.Format(LanguageOper.ReadValue(section, "msgStr.9"), forceInitSendNum);
            }

            this.BeginInvoke(printLogDelegate, msg);
        }

        // 打开升级文件
        private void openUpdateSrcFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Bin Files(*.bin)|*.bin";
            o.Multiselect = false;
            if (Directory.Exists(parent.lastPath_ReceiverControl_UpdateSrcFile))
            {
                o.InitialDirectory = parent.lastPath_ReceiverControl_UpdateSrcFile;
            }
            else
            {
                o.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            }
            DialogResult dr = o.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string tmpSrcPath = o.FileName;
                string flagStr = tmpSrcPath.Substring(tmpSrcPath.LastIndexOf("\\") + 1);	// 去掉了路径
                if (preReadUpdateSrcFile(tmpSrcPath) == false)
                {
                    string msg = "文件格式错误, 请检查";
                    if (LanguageOper.available == true)
                    {
                        msg = LanguageOper.ReadValue(section, "msgStr.10");
                    }

                    MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return;
                }
                else
                {
                    updateSrcFilePath = tmpSrcPath;
                    updateSrcFileTextBox.Text = updateSrcFilePath;
                    updateSrcFileTextBox.SelectionStart = updateSrcFileTextBox.TextLength;
                    updateSrcFileTextBox.ScrollToCaret();
                }

                parent.lastPath_ReceiverControl_UpdateSrcFile = (new FileInfo(o.FileName)).DirectoryName;
            }
        }

        //升级放大器
        private void updateReceiverButton_Click(object sender, EventArgs e)
        {
            if (updateSrcFilePath.Equals(""))
            {
                string msg = "请选择升级文件";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.11");
                }

                MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (!TestFlag)
            {
                initModeRadioButton.Select();
                setUpdateModeButton_Click(setUpdateModeButton, new EventArgs());
                Thread.Sleep(50);
            }

            if (updateMcuProcessing == false)
            {
                if (getCommonParameter() == false)
                {
                    return;
                }

                Thread t = new Thread(() =>
                {
                    this.BeginInvoke(startUpdateMcuDelegate);
                    updateMcu(updateSrcFilePath, mcuMode);
                });
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                string msg = "确定停止升级操作吗？";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.12");
                }

                DialogResult dr = MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dr == System.Windows.Forms.DialogResult.Yes)
                {
                    forceStopUpdateMcu = true;
                }
            }

            
        }
        // 查询放大器信息
        private void getReceiverInfoButton_Click(object sender, EventArgs e)
        {
            if (getCommonParameter() == false)
            {
                return;
            }
            string ampVersion = baseCoreNet.getAmpVersion(subIndex, portIndex)[0];
            string msg = String.Format("当前固件版本：{0}", ampVersion);
            if (LanguageOper.available == true)
            {
                msg = String.Format(LanguageOper.ReadValue(section, "msgStr.15")+"\n", ampVersion);
            }
            this.BeginInvoke(printLogDelegate, msg);
        }

        // 窗口关闭前处理
        private void Form_ReceiverControl_FormClosing(object sender, FormClosingEventArgs e)
        {
            forceInitTimer.Stop();
            baseCoreNet.setControlState(subIndex, false, false);
            //TOTEST
        }

        // 分控 - 全局/定点
        private void allSubCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            subLabel.Enabled = !allSubCheckBox.Checked;
            subIndexTextBox.Enabled = !allSubCheckBox.Checked;
        }

        // 端口 - 全局/定点
        private void allPortCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            portLabel.Enabled = !allPortCheckBox.Checked;
            portIndexTextBox.Enabled = !allPortCheckBox.Checked;
        }

        // 解码器 - 全局/定点
        private void allMiniCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            miniLabel.Enabled = !allMiniCheckBox.Checked;
            miniIndexTextBox.Enabled = !allMiniCheckBox.Checked;
        }

        // 升级模式 - 32
        private void updateMode32RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            mcuMode = 1;
        }

        // 升级模式 - 8
        private void updateMode8RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            mcuMode = 2;
        }

        // 升级模式 - N
        private void n76E003Button_CheckedChanged(object sender, EventArgs e)
        {
            mcuMode = 3;
        }

        // 清空日志
        private void clearLogButton_Click(object sender, EventArgs e)
        {
            logRichTextBox.Text = "";
        }

        /**********************************************************************************************/

        // 打印日志
        private void printLog(object o)
        {
            string message = String.Format("[{0}-{1}-{2} {3}] {4}\n", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.ToLongTimeString(), o.ToString());
            logRichTextBox.AppendText(message);
            logRichTextBox.SelectionStart = logRichTextBox.TextLength;
            logRichTextBox.ScrollToCaret();
        }

        // 更新解码器升级进度
        private void updateMcuUpdateProcess(int value)
        {
            if (value >= 0 && value <= 100)
            {
                updateMcuProgressBar.Value = value;
                updateMcuProcessLabel.Text = value + "%";
            }
        }

        // 预读升级文件
        private bool preReadUpdateSrcFile(string srcFilePath)
        {
            try
            {
                if (!File.Exists(srcFilePath))
                {
                    return false;
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        // 升级解码器 - 前处理
        private void startUpdateMcu()
        {
            subGroupBox.Enabled = false;
            portGroupBox.Enabled = false;
            recGroupBox.Enabled = false;
            controlGroupBox.Enabled = false;
            getInfoGroupBox.Enabled = false;

            getReceiverInfoButton.Enabled = false;
            sendForceUpadteCmdButton.Enabled = false;
            setUpdateModeButton.Enabled = false;

            openUpdateSrcFileButton.Enabled = false;
            //updateMode32RadioButton.Enabled = false;
            //updateMode8RadioButton.Enabled = false;

            updateMcuProgressBar.Value = 0;
            updateMcuProcessLabel.Text = "0%";

            string text = "停止升级";
            if (LanguageOper.available == true)
            {
                text = LanguageOper.ReadValue(section, "updateReceiverButton.Text.0");
            }
            updateReceiverButton.Text = text;
            forceStopUpdateMcu = false;
        }

        // 弹窗提示升级后的信息
        private void alertUpdateDoneMessage()
        {
            if (forceStopUpdateMcu == false)
            {
                string msg = "解码器升级完成";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.13");
                }
                MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                string msg = "升级操作已停止";
                if (LanguageOper.available == true)
                {
                    msg = LanguageOper.ReadValue(section, "msgStr.14");
                }
                MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            updateMcuUpdateProcess(0);

            subGroupBox.Enabled = true;
            portGroupBox.Enabled = true;
            recGroupBox.Enabled = true;
            controlGroupBox.Enabled = true;
            getInfoGroupBox.Enabled = true;

            getReceiverInfoButton.Enabled = true;
            sendForceUpadteCmdButton.Enabled = true;
            setUpdateModeButton.Enabled = true;

            openUpdateSrcFileButton.Enabled = true;
            //updateMode32RadioButton.Enabled = true;
            //updateMode8RadioButton.Enabled = true;

            string text = "升级";
            if (LanguageOper.available == true)
            {
                text = LanguageOper.ReadValue(section, "updateReceiverButton.Text.1");
            }
            updateReceiverButton.Text = text;
            forceStopUpdateMcu = false;
        }

        // 升级放大器 - 逻辑动作
        private void updateMcu(string srcFilePath, int mode)
        {
            updateMcuProcessing = true;

            try
            {
                FileStream fs = new FileStream(srcFilePath, FileMode.Open);
                BinaryReader reader = new BinaryReader(fs);

                int dataLength = (int)fs.Length;
                byte[] dataBuff = new byte[65536];
                reader.Read(dataBuff, 0, dataLength);
                reader.Close();

                /*
                int totalLength = 0;
                if (dataLength % 0x400 > 0)
                {
                    totalLength = ((dataLength >> 10) + 1) << 10;
                }
                else
                {
                    totalLength = dataLength;
                }
                */
                //TOTEST
                AmpUpgradeActive(subIndex, portIndex, dataLength, dataBuff);
                /*
                if (mode == 1)
                {
                    mcu32UpgradeActive(subIndex, portIndex, miniIndex, dataLength, dataBuff);
                }
                else if (mode == 2)
                {
                    mcu8UpgradeActive(subIndex, portIndex, miniIndex, dataLength, dataBuff);
                }
                else if (mode == 3)
                {
                    mcuN76E003UpgradeActive(subIndex, portIndex, miniIndex, dataLength, dataBuff);
                }
                 * */
                

                this.BeginInvoke(alertUpdateDoneMessageDelegate);
            }
            catch (Exception ex)
            {
                this.BeginInvoke(printLogDelegate, ex.Message + ex.StackTrace);
            }

            updateMcuProcessing = false;
            if (!TestFlag)
            {
                ResetToActive();
            }
        }

        private void ResetToActive()
        {
            if (InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate()
                {
                    this.ResetToActive();
                }));
            }
            else
            {
                Thread.Sleep(50);
                activeModeRadioButton.Checked = true;
                setUpdateModeButton_Click(setUpdateModeButton, new EventArgs());
            }

        }

        // 放大器升级 - 逻辑动作
        private void AmpUpgradeActive(int sub_index, int sub_port, int dataLength, byte[] dataBuff)
        {
            byte[] memData = new byte[1024];
            int blockLength = 0x80;

            // 发送数据到SRAM
            int sentLength = 0;
            for (int i = 0; i < dataLength; i += blockLength)
            {
                for (int k = 0; k < blockLength; k++)
                {
                    memData[k] = dataBuff[i + k];
                }

                baseCoreNet.setAmpReceive(sub_index, sub_port, i, memData);

                sentLength += blockLength;
                this.BeginInvoke(updateMcuUpdateProcessDelegate, sentLength * 100 / dataLength);

                Thread.Sleep(100);

                // 响应强制停止
                if (forceStopUpdateMcu)
                {
                    break;
                }
            }
            Thread.Sleep(70);
        }


        // 窗体加载后处理
        private void Form_ReceiverControl_Load(object sender, EventArgs e)
        {
            /*
            // 复位解码器
            if (resetModeRadioButton.Checked)
            {
                baseCoreNet.setMcuReset(subIndex, portIndex, miniIndex);
                this.BeginInvoke(printLogDelegate, "设置解码器模式：Reset");
            }

            // 释放解码器控制权
            baseCoreNet.setControlState(subIndex, false, false);
            */
        }

        // 关闭窗体
        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form_AmpControl_FormClosing(object sender, FormClosingEventArgs e)
        {
            baseCoreNet.setLampCheckProcessing(false);
            baseCoreNet.setControlState(subIndex, false, false);
        }

        private void TestModeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            TestFlag = TestModeCheckBox.Checked;
            if (TestFlag)
            {
                controlGroupBox.Enabled = true;
                modeGroupBox.Enabled = true;
            }
            else
            {
                controlGroupBox.Enabled = false;
                modeGroupBox.Enabled = false;
            }
        }
    }
}
